<template>
  <el-dialog
    title="修改密码"
    width="30%"
    :visible.sync="passwordVisible"
    :close-on-click-modal="false"
    :modal="false"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      label-width="80px"
      :rules="dataFormRules"
      :size="size"
    >
      <el-form-item label="旧密码" prop>
        <el-input v-model="dataForm.oldPassword" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item label="新密码" prop>
        <el-input v-model="dataForm.newPassword" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item label="确认密码" prop>
        <el-input v-model="dataForm.confirmPassword" type="password" auto-complete="off" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="passwordVisible = false">取消</el-button>
      <el-button size="small" type="primary" @click="changePassword">确认</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'MessagePanel',
  data() {
    return {
      dataForm: {
        oldPassword: null,
        newPassword: null,
        confirmPassword: null
      },
      passwordVisible: false,
      size: 'small',
      dataFormRules: {
        label: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    setPasswordVisible: function(passwordVisible) {
      this.passwordVisible = passwordVisible
    },
    changePassword: function() {
      // 首先判断新密码和确认密码是否一致
      // TODO 若不一致，返回并给出提示
      if (this.dataForm.newPassword != this.dataForm.confirmPassword) {
        this.$message({
          message: '两次输入的密码不一致',
          type: 'error'
        })
        return
      }
      // TODO 若一致，则向后台发请求，参数是用户名、新密码、旧密码
      // let params = Object.assign({}, this.dataForm)
      this.$api.user.changePassword(this.dataForm).then((res) => {
        if (res.code == 500) {
          this.$message({
            message: res.msg,
            type: 'error'
          })
        } else {
          this.$message({
            message: res.msg,
            type: 'success'
          })
          this.passwordVisible = false
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
